//run.cpp
#include <iostream>
#include <string>
#include <ctime>
#include "Student.hpp"
using namespace std;
void bubbleSort(Stduent arr[], int len);//使用冒泡排序对学生排序

int main() {
    Stduent* stduents = new Stduent[30];
    srand((int)time(0));
    for (int j = 0; j < 2; j++) {//测试六次
        cout << "第" << j + 1 << "次测试" <<endl;
        for (int i = 0; i < 30; i++) { //初始化学生信息
            string str = "学生" + to_string(i + 1) + "号";
            stduents[i].setName(str);
            stduents[i].setIdNumber(i + 1);
            stduents[i].setScore((rand() % (101 - 40))+ 40); //最低分给40分
        }
        
        // for (int i = 0; i < 30; i++) {
        //     cout << "姓名：" << stduents[i].getName() <<
        //     "，学号：" << stduents[i].getIdNumber() <<
        //     "，分数" << stduents[i].getScore() << endl;
        // }
        
        bubbleSort(stduents, 30);
        double sumOfScore = 0;//总分
        for (int i = 0; i < 30; i++) {
            sumOfScore += stduents[i].getScore();
        }

        double av = sumOfScore / 30;//平均分
        
        cout << "平均分为：" << av << endl;
        for (int i = 0; i < 30; i++) {
            //按成绩高到低顺序输出高于平均成绩的学生信息
            if (stduents[i].getScore() < av) {
                break;
            }
            cout << "姓名：" << stduents[i].getName() <<
            "，学号：" << stduents[i].getIdNumber() <<
            "，分数" << stduents[i].getScore() << endl;
        }
    }
    return 0;
}

void bubbleSort(Stduent arr[], int len) {//冒泡的实现
    Stduent *temp = new Stduent();
    for (int i = 0; i < len - 1; i++)
        for (int j = 0; j < len - 1 - i; j++)
        if (arr[j].getScore() < arr[j + 1].getScore())
        {
            temp->setIdNumber(arr[j].getIdNumber());
            temp->setName(arr[j].getName());
            temp->setScore(arr[j].getScore());

            arr[j].setIdNumber(arr[j + 1].getIdNumber());
            arr[j].setName(arr[j + 1].getName());
            arr[j].setScore(arr[j + 1].getScore());

            arr[j + 1].setIdNumber(temp->getIdNumber());
            arr[j + 1].setName(temp->getName());
            arr[j + 1].setScore(temp->getScore());
        }
        delete temp;
}